<?php 

namespace App\Repositories;

class ContractRepository extends Repository 
{
    public function model() 
    {
        return 'App\Models\Contract';
    }

    /**
     * @param int $perPage
     * @param array $columns
     * @return mixed
     */
    public function paginateList($where=[], $page = 20)
    {
        $where = $where->filter(function ($item) { return $item; });
        
        $model = $this->model->with('customer');

        if ($where->has('word')) {
    
            $word = trim($where->get('word'));
            $model->where('contract_no', 'like', "%{$word}%");
        }

        return $model->paginate($page);
    }

    // 检查合同完整性
    public function checkFull($contractNo, $customerId)
    {
        $contract = $this->findBy('contract_no', $contractNo);

        if (!$contract) {
            return back()->with('error', '对不起，你填写的合同不存在。');
        }

        if (!$contract->cid) {
            $contract->cid = $customerId;
            $contract->save();
        }

        return true;
    }
}